package cn.edu.bzu.ie.sf.mealsmisweb.mapper;

import cn.edu.bzu.ie.sf.mealsmisweb.entity.Department;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.mapper
 * @Description : 部门信息数据库操作映射接口
 * @Create on : 2021/8/10 10:12
 * @author: Feng Song 宋锋
 **/
@Repository
public interface DepartmentMapper extends Mapper<Department> {
    /**
     * 获取所有的部门信息-支队管理、用户注册、用户添加时的查询
     * @return List<Department> 实例
     */
    @Select("select a.*, b.depname as pname, b.deptype as ptype, c.dname, d.uname as creator_name " +
            " from department a " +
            " left join department b on a.pid=b.id" +
            " left join district c on a.distid=c.id" +
            " left join userinfo d on a.creator=d.id")
    List<Department> getAllDepartmentList();

    /**
     * 获取某个部门下的所有部门信息-用于大队用户查看管理本部门下的部门信息
     * @param pid 父部门的id
     * @return List<Department> 实例
     */
    @Select("select a.*, b.depname as pname, b.deptype as ptype, c.dname, d.uname as creator_name " +
            " from department a " +
            " left join department b on a.pid=b.id" +
            " left join district c on a.distid=c.id" +
            " left join userinfo d on a.creator=d.id where a.pid=#{pid} or a.id=#{pid}")
    List<Department> getAllDepartmentListByPid(int pid);

    /**
     * 获取所有的你级部门，deptype=0或1，用于部门管理中部门添加、修改
     * @return List<Department> 实例
     */
    @Select("select a.*, b.depname as pname, b.deptype as ptype, c.dname, d.uname as creator_name " +
            " from department a " +
            " left join department b on a.pid=b.id" +
            " left join district c on a.distid=c.id" +
            " left join userinfo d on a.creator=d.id where a.deptype<2")
    List<Department> getAllParentDepartmentList();

    /**
     * 获取某个部门信息
     * @param id 部门的id
     * @return Department 实例
     */
    @Select("select a.*, b.depname as pname, b.deptype as ptype, c.dname, d.uname as creator_name " +
            " from department a " +
            " left join department b on a.pid=b.id" +
            " left join district c on a.distid=c.id" +
            " left join userinfo d on a.creator=d.id where a.id=#{id}")
    Department getDepartmentById(int id);

    /**
     * 添加部门信息
     * @param item 要添加的部门信息
     * @return int 影响的行数
     */
    @Insert("insert into department(depname, pid, deptype, city, distid, creator, create_dt) " +
            " values(#{depname}, #{pid}, #{deptype}, #{city}, #{distid}, #{creator}, #{create_dt})")
    int addDepartment(Department item);

    /**
     * 修改部门信息
     * @param item 要修改的部门信息
     * @return int 影响的行数
     */
    @Update("update department set depname=#{depname}, pid=#{pid}, deptype=#{deptype}, distid=#{distid} where id=#{id}")
    int editDepartment(Department item);

    /**
     * 删除部门信息
     * @param id 要删除部门的id
     * @return int 影响的行数
     */
    @Delete("delete from department where id=#{id}")
    int delDepartment(int id);
}
